<template>
    <div class="e-form">
        <BillTop title="租赁退场新增" @cancel="handleClose"></BillTop>
        <div class="tabs">
            <div class="tabs-title">基本信息</div>
            <el-form ref="form" :model="formData.handoverLeaseExit" label-width="180px" :rules="rules">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="退场日期：" prop="billDate">
                            <el-date-picker
                                v-model="formData.handoverLeaseExit.billDate"
                                type="date"
                                placeholder="选择日期"
                                value-format="yyyy-MM-dd"
                                :clearable="false"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="退场人：">
                            <span> {{formData.handoverLeaseExit.founderName}}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="接收单位：" prop="supplierName">
                            <el-input v-model="formData.handoverLeaseExit.supplierName"  readonly>
                                <i slot="suffix" class="el-input__icon el-icon-document-copy" @click="selectSupplier"></i>
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                   <el-col :span="12">
                        <el-form-item label="机构：">
                            <span> {{userInfo.orgInfo.orgName}}</span>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="状态：">
                          <span>{{ statusLable(formData.handoverLeaseExit.state) }}</span>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                     <el-col :span="24">
                        <el-form-item  label="退场情况：">
                            <el-input type="textarea" v-model="formData.handoverLeaseExit.remarks" placeholder="请输入内容"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </div>
        <div class="buttons">
            <el-button
                type="primary"
                size="small"
                class="btn-blue"
                @click="preserve"
                v-debounce
                >保存</el-button
            >
            <el-button size="small" @click="handleClose" v-debounce>取消</el-button>
        </div>
    </div>
</template>

<script>
import request from '@/api/equip/purchaseAcceptance.js'
import { mapState } from 'vuex'
import { selectLeaseExitOrg } from '@/api/equip/popupCallback.js'
export default {
    data () {
        return {
            formData: {
                //基础信息
                'handoverLeaseExit': {
                    'auditDate': '',
                    'auditor': '',
                    'auditorId': '',
                    'billDate': '',
                    'billId': '',
                    'billNo': '',
                    'exitCondition': '',
                    'founderId': '',
                    'founderName': '',
                    'gmtCreate': '',
                    'gmtModified': '',
                    'leaseAcceptanceId': '',
                    'leaseAcceptanceNo': '',
                    'modifier': '',
                    'modifierId': '',
                    'nullifyCreated': '',
                    'nullifyCreator': '',
                    'nullifyCreatorId': '',
                    'nullifyDescription': '',
                    'nullifyReason': '',
                    'orgId': '',
                    'orgName': '',
                    'recorder': '',
                    'recorderId': '',
                    'remarks': '',
                    'state': 0,
                    'supplierId': '',
                    'supplierName': '',
                    'workId': ''
                },
            },
            rules: {
                billDate: [
                    { required: true, message: '请选择退场日期', trigger: 'change' },
                ],
                supplierName: [
                    { required: true, message: '请选择接收单位', trigger: 'change'  },
                ],
            },
        }
    },
    components: {
    },
    computed: {
        ...mapState({
            userInfo: state => state.userInfo,
        }),
        statusLable () {
            return param=> {
                const state =  this.$store.state.equip.equipData.state
                const obj = state.find(x=>x.value === param)
                return obj.label
            }
        },
    },
    created () {
        this.getInfo()
    },
    mounted () {

    },
    methods: {
        getInfo () {
            const date = new Date()
            this.formData.handoverLeaseExit.billDate = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
            this.formData.handoverLeaseExit.founderName = localStorage.getItem('userName')
        },
        //选择接收单位
        selectSupplier () {
            let ids = [this.formData.handoverLeaseExit.leaseAcceptanceId]
            selectLeaseExitOrg(ids).then(res=>{
                res.forEach(item=>{
                    this.formData.handoverLeaseExit.supplierName = item.leaseOrgName
                    this.formData.handoverLeaseExit.supplierId = item.leaseOrgId
                    this.formData.handoverLeaseExit.leaseAcceptanceId = item.id
                })
            })
        },
        //保存
        preserve () {
            this.$refs.form.validate(valid => {
                if(valid) {
                    request.leaseExitAdd(this.formData).then(res=>{
                        this.clientPop('info', '保存成功!是否进行下一步操作？', ()=>{
                            this.$router.push({
                                path: '/leaseExitEdit',
                                query: {
                                    billid: res,
                                    name: 'planDetail'
                                }
                            })
                        },
                        null,
                        this.handleClose
                        )
                    })
                }else{
                    return false
                }
            })
        },
        //取消
        handleClose () {
            this.$router.replace('/leaseExitList')
        },
    }
}
</script>

<style lang="scss" scoped>
.buttons {
    left: 0;
}
.e-form {
    padding: 0 20px;
    .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    }
}
</style>
